<?
	// ****************************************
	// For using FTA Flash, you need:
	// -- /html/image-feed.php
	// -- add this in .htaccess - RewriteRule ^images/imageFeed.xml$ /html/image-feed.php [L,NE]
	// -- /images/home.swf (file name/path can be changed in /site/fta/config.php)
	// -- use right function to feed images to image-feed.php
	// ****************************************

	class FTAFlashGallery extends FTAObject {
		
		var $cmsDocument;
		
		// we might have more than one swf file...
		var $swfFile;
		var $swfFile2;
		var $swfFile3;
		
		var $swfWidth;
		var $swfHeight;
		var $swfFlashPlayer;
		
		var $imagesForswf;
		
		var $swfDivId;
		var $swfmenu;
		var $swfwmode;
		
				
		function FTAFlashGallery() {
			parent::FTAObject();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTAswfFile;
			$this->swfFile = (isset($FTAswfFile)) ? $FTAswfFile : "/images/flashGallery.swf";
			
			global $FTAswfFile2;
			$this->swfFile2 = (isset($FTAswfFile2)) ? $FTAswfFile2 : "/images/flashGallery2.swf";
			
			global $FTAswfFile3;
			$this->swfFile3 = (isset($FTAswfFile3)) ? $FTAswfFile3 : "/images/flashGallery3.swf";
			
			global $FTAswfWidth;
			$this->swfWidth = (isset($FTAswfWidth)) ? $FTAswfWidth : "325";
			
			global $FTAswfHeight;
			$this->swfHeight = (isset($FTAswfHeight)) ? $FTAswfHeight : "325";
			
			global $FTAswfFlashPlayer;
			$this->swfFlashPlayer = (isset($FTAswfFlashPlayer)) ? $FTAswfFlashPlayer : "8";
			
			global $FTAswfDivId;
			$this->swfDivId = (isset($FTAswfDivId)) ? $FTAswfDivId : "flashGallery";
			
			global $FTAswfmenu;
			$this->swfmenu = (isset($FTAswfmenu)) ? $FTAswfmenu : "false";
			
			global $FTAswfwmode;
			$this->swfwmode = (isset($FTAswfwmode)) ? $FTAswfwmode : "transparent";
								
		}
		
		
		// this is for the situation when you have 2-3 image fields in one document
		// $documentIdentifier must be int and not string
		function getArrayOfImagesFromSameDoc($documentIdentifier, $filetypecode, $db){
			$this->imagesForswf = null;
			
			$q = "SELECT id, documentcode, elementid, filetypecode, filename, mimetype, filesize, filename FROM binarydata WHERE documentcode=?";
			$f = array($documentIdentifier);
		    $db->prepareAndExecuteQuery($q,$f);	
		    		    		    
		    if($db->numRows() > 0 ){
		    	$c = $db->numRows();
		    	$imagesArray = array();
		    	for ($i=0; $i < $c; $i++) {
		    		if($row = $db->nextRow()){
		    			$imageSrc = "/html/blob.php?documentCode=" . $row[1] . "&elementId=" . $row[2] . "&filetypecode=" . $filetypecode;
		    			array_push($imagesArray, $imageSrc);
		    		}
		    	}
		    	
		    	
		    	if(count($imagesArray) > 0){
		    		$this->imagesForswf = $imagesArray;
		    	} 
		    }
			return $this->imagesForswf;
		}
		
				
		// this is when you have images in an image gallery, so we give this function the categorycode
		// because we will be getting images from image gallery, so documenttype will always be docFileMod
		// $categorycode must be int and not string
		function getArrayOfImagesFromGalleryCategory($categoryIdentifier, $elementId, $filetypecode){
			$this->imagesForswf = null;
			 
			$this->cmsDocument->flush();
			$this->cmsDocument->addDocumentType("docFileMod");
			$this->cmsDocument->addConstraint("categorycode", $categoryIdentifier);
			$this->cmsDocument->addConstraint("author", 0);
			$this->cmsDocument->addSort("<<rank>>");
			$this->cmsDocument->setPageSize(null);	
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() > 0){
				$allImages = $this->cmsDocument->getAllDocuments();
				$imagesArray = array();
				foreach($allImages as $row){
					$imageSrc = "/html/blob.php?documentCode=" . $row->documentcode . "&elementId=" . $elementId . "&filetypecode=" . $filetypecode;
					array_push($imagesArray, $imageSrc);
				}
				
				if(count($imagesArray) > 0){
		    		$this->imagesForswf = $imagesArray;
		    	} 
			}
			return $this->imagesForswf;
		}		
		
		
		function renderFlash(){
?>			
			<script type="text/javascript" src="/js/swfobject.js"></script>
			<script type="text/javascript">
				var so = new SWFObject("<?= $this->swfFile; ?>", "", "<?= $this->swfWidth; ?>", "<?= $this->swfHeight; ?>", "<?= $this->swfFlashPlayer; ?>");
				so.addParam("menu","<?= $this->swfmenu; ?>");
				so.addParam("wmode","<?= $this->swfwmode; ?>");
				so.write("<?= $this->swfDivId; ?>");			
				</script>
<?				
		}
		
		
		function renderImagesSrc(){
			if(!is_array($this->imagesForswf)){
				return;
			} else {	
				return $this->imagesForswf;
			}				
		}
	}
	
	
	//------------------------------------	
		
	// when images are in same document
	function fta_getImagesForFlashFromSameDoc($documentIdentifier, $filetypecode, $db){
		$ftaFlash = &fta_getSharedFlashGalleryObject();		
		$ftaFlash->getArrayOfImagesFromSameDoc($documentIdentifier, $filetypecode, $db);
		return $ftaFlash->renderImagesSrc();
	}
	
	// when images are in an image gallery
	function fta_getImagesForFlashFromImageGallery($categoryIdentifier, $elementId, $filetypecode){
		$ftaFlash = &fta_getSharedFlashGalleryObject();		
		$ftaFlash->getArrayOfImagesFromGalleryCategory($categoryIdentifier, $elementId, $filetypecode);
		return $ftaFlash->renderImagesSrc();
	}
	
	// get script for flash
	function fta_getFlashScript(){
		$ftaFlash = &fta_getSharedFlashGalleryObject();		
		return $ftaFlash->renderFlash();
	}
		
	//------------------------------------
	
	function &fta_getSharedFlashGalleryObject(){

		global $fta_;

		global $FTAFlashGalleryClass;
		
		if(!is_null($FTAFlashGalleryClass) && "FTAFlashGallery" != $FTAFlashGalleryClass){
		} else {
			$FTAFlashGalleryClass = "FTAFlashGallery";
		}
			
		if (!array_key_exists('ftaFlashGallery',$fta_)){
			$fta_->ftaFlashGallery = new  $FTAFlashGalleryClass();
		}
		return $fta_->ftaFlashGallery;
	}